﻿using System.Web.Http;
using System.Threading.Tasks;
using System;
using Top.Core;
using Top.Web.Api;
using Top.PointStore.Business;
using Top.PointStore.Web.Filters;
using Top.Common;

namespace Top.PointStore.Web.Controllers
{
    /// <summary>
    /// 积分规则 API
    /// </summary>
    public class PointRuleController : WebApi
    {
        /// <summary>
        /// 添加积分规则 
        /// </summary>
        /// <param name="pointCode">行为标识，默认为空</param>
        /// <param name="pointAction">触发行为，默认为空</param>
        /// <param name="point">积分数，默认为0</param>
        /// <param name="status">启用状态，默认为0</param>
        /// <param name="sort">排序，默认为0</param>
        /// <returns>添加成功返回true</returns>
        [HttpPost]
        public async Task<bool> Create
            (string pointCode = "", string pointAction = "", int point = 0,
            int status = 0, int sort = 0)
        {
            SystemManager.AddLog(User.CrmUserID, "添加积分规则", "用户名：" + User.Name).Wait();
            return await PointRuleManager.Create(pointCode, pointAction, point,
                status, sort, User.CrmUserID);
        }

        /// <summary>
        /// 删除积分规则
        /// </summary>
        /// <param name="pointRuleKidList">积分规则ID列表</param>
        /// <returns>删除成功返回true</returns>
        public async Task<bool> Delete(int pointRuleKid)
        {
            SystemManager.AddLog(User.CrmUserID, "删除积分规则", "用户名：" + User.Name).Wait();
            return await PointRuleManager.Delete(pointRuleKid);
        }

        /// <summary>
        /// 编辑积分规则
        /// </summary>
        /// <param name="pointRuleKid">积分规则ID，默认为0</param>
        /// <param name="pointCode">行为标识，默认为空</param>
        /// <param name="pointAction">触发行为，默认为空</param>
        /// <param name="point">积分，默认为0</param>
        /// <param name="status">启用状态，默认为false</param>
        /// <param name="sort">排序，默认为0</param>
        /// <returns>编辑成功返回true</returns>
        [HttpPost]
        public async Task<bool> Edit(int pointRuleKid = 0, string pointCode = "",
            string pointAction = "", int point = 0, bool status = false, int sort = 0)
        {
            SystemManager.AddLog(User.CrmUserID, "编辑积分规则", "用户名：" + User.Name).Wait();
            return await PointRuleManager.Edit(pointRuleKid, pointCode, pointAction, point,
                status, sort, User.CrmUserID);
        }

        /// <summary>
        /// 获取一条积分规则
        /// </summary>
        /// <param name="pointRuleKid">积分规则id</param>
        /// <returns></returns>
        public async Task<T_PointRule> Retrieve(int pointRuleKid)
        {
            return await PointRuleManager.Retrieve(pointRuleKid);
        }

        /// <summary>
        /// 积分规则列表
        /// </summary>
        /// <param name="keyword">搜索关键字，默认为空</param>
        /// <param name="pageIndex">初始页码，默认为1</param>
        /// <param name="pageSize">页面大小，默认为20</param>
        /// <returns>积分规则分页列表</returns>
        public async Task<PagingList<T_PointRule>> List
            (string keyword = "", int pageIndex = 1, int pageSize = 20)
        {
            pageIndex = pageIndex <= 0 ? 1 : pageIndex;
            pageSize = pageSize <= 0 ? 20 : pageSize;
            return await PointRuleManager.List(keyword, pageIndex, pageSize);
        }

        /// <summary>
        /// 修改积分规则状态
        /// </summary>
        /// <param name="pointRuleKid">积分规则ID，默认为0</param>
        /// <param name="status">启用状态，默认为false</param>
        /// <returns>修改成功返回true</returns>
        [HttpPost]
        public async Task<bool> ModifyStatus(int pointRuleKid = 0, bool status = false)
        {
            SystemManager.AddLog(User.CrmUserID, "编辑积分规则", "用户名：" + User.Name).Wait();
            return await PointRuleManager.ModifyStatus(pointRuleKid, status);
        }
    }
}
